﻿using FastCloud.Base;
using FastCloud.Extend.DB;
using FastCloud.Extend.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Extend
{
    [Route("frame/extend/theme")]
    public class ThemeController : BaseController
    {
        [AutoWired]
        private DB_Theme DB_Theme;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Extend/Theme/Theme_List.cshtml");
        }
        [Route("add")]
        public IActionResult Add()
        {
            return View("~/Views/Extend/Theme/Theme_Add.cshtml");
        }
        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Extend/Theme/Theme_Edit.cshtml");
        }
        [Route("setting")]
        public IActionResult Setting()
        {
            return View("~/Views/Extend/Theme/Theme_Setting.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ThemeName = ValueConvert.ConvertToString(GetFormParam("ThemeName"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(ThemeName))
            {
                sql += " and ThemeName like @ThemeName";
                ParamList.Add(new ParamEntity() { ParamName = "ThemeName", ParamValue = "%" + ThemeName + "%" });
            }

            return Result.OK(DB_Theme.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("opsettinglist"), HttpPost]
        public IActionResult OpSettingList()
        {
            return Result.OK(DB_Theme.SelectAllList("*", "1=1"));
        }

        [Route("opinsert"), HttpPost]
        public IActionResult OpInsert(Frame_Theme Entity)
        {
            DB_Theme.Insert(Entity);
            return Result.OK();
        }

        [Route("opupdate"), HttpPost]
        public IActionResult OpUpdate(Frame_Theme Entity)
        {
            DB_Theme.UpdateByID(Entity);
            return Result.OK();
        }

        [Route("opdetail"), HttpPost]
        public IActionResult OpDetail()
        {
            string ThemeGuid = ValueConvert.ConvertToString(GetFormParam("ThemeGuid"));
            return Result.OK(DB_Theme.SelectOne(ThemeGuid));
        }

        [Route("opbatchdelete"), HttpPost]
        public IActionResult OpBatchDelete()
        {
            string KeyID = ValueConvert.ConvertToString(GetFormParam("KeyID"));
            List<string> KeyList = KeyID.TrimEnd(';').Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            DB_Theme.DeleteBatch(KeyList);
            return Result.OK();
        }
    }
}
